package com.lagou;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.Set;

public class JedisClusterTest {
    public static void main(String[] args) {
        String host = "172.17.0.4";
        JedisPoolConfig config = new JedisPoolConfig();
        Set<HostAndPort> jedisClusterNode = new HashSet<>();

        jedisClusterNode.add(new HostAndPort(host, 7001));
        jedisClusterNode.add(new HostAndPort(host, 7002));
        jedisClusterNode.add(new HostAndPort(host, 7003));
        jedisClusterNode.add(new HostAndPort(host, 7004));

        jedisClusterNode.add(new HostAndPort(host, 8001));
        jedisClusterNode.add(new HostAndPort(host, 8002));
        jedisClusterNode.add(new HostAndPort(host, 8003));
        jedisClusterNode.add(new HostAndPort(host, 8004));

        JedisCluster jedisCluster = new JedisCluster(jedisClusterNode, config);

        // 向redis集群写数据
        jedisCluster.set("name", "zhangsan");
        jedisCluster.set("age", "18");

        for (int i = 1; i <= 10; i++) {
            jedisCluster.set("name" + i, "zhang" + i);
        }

        // 向redis集群读数据
        System.out.println("name: " + jedisCluster.get("name"));
        System.out.println("age: " + jedisCluster.get("age"));
        for (int i = 1; i <= 10; i++) {
            String value = jedisCluster.get("name" + i);
            System.out.println("name" + i + ": " + value);
        }


    }
}
